<?php

namespace App\Observers;

use App\Models\Device;
use App\Models\VisitWebTask;
use Carbon\Carbon;

class VisitWebTaskObserver
{
    /**
     * Handle the VisitLog "created" event.
     *
     * @param \App\Models\VisitWebTask $model
     * @return void
     */
    public function created(VisitWebTask $model)
    {

    }

    /**
     * Handle the VisitLog "updated" event.
     *
     * @param \App\Models\VisitLog $model
     * @return void
     */
    public function updated(VisitWebTask $model)
    {
        if ($model->status==VisitWebTask::STATUS_DONE){
            $device = Device::query()->where(['device_id' => $model->device_id])->first();
            $device->visit_web_at = Carbon::now();
            $device->count_visit_web=VisitWebTask::query()->where('device_id',$model->device_id)->where('status',VisitWebTask::STATUS_DONE)->count();
            $device->save();
        }
        //
    }

    /**
     * Handle the VisitWebTask "deleted" event.
     *
     * @param \App\Models\VisitWebTask $model
     * @return void
     */
    public function deleted(VisitWebTask $model)
    {
        //
    }

    /**
     * Handle the VisitWebTask "restored" event.
     *
     * @param \App\Models\VisitWebTask $model
     * @return void
     */
    public function restored(VisitWebTask $model)
    {
        //
    }

    /**
     * Handle the VisitWebTask "force deleted" event.
     *
     * @param \App\Models\VisitWebTask $model
     * @return void
     */
    public function forceDeleted(VisitWebTask $model)
    {
        //
    }

    public function creating(VisitWebTask $model)
    {

    }

    public function saving(VisitWebTask $model)
    {

    }
}
